﻿'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  console.log('【createTaskNotification】云函数被调用，event：', event);

  const { 
    userId, 
    taskId, 
    taskTitle, 
    status, 
    actionUserId, 
    actionUserNickname,
    extraData // 新增：额外数据
  } = event;

  try {
    let title = '';
    let content = '';
    let actionUrl = '';
    let actionText = '';

    // 根据任务状态生成不同的通知内容
    switch (status) {
      case 'accepted':
        title = '任务被领取';
        content = '您发布的任务"' + taskTitle + '"已被用户' + actionUserNickname + '领取';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '查看任务';
        break;
      case 'claimed':
        title = '任务领取成功';
        content = '您已成功领取任务"' + taskTitle + '"，请注意按时完成，避免超时';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '查看任务';
        break;
      case 'completed':
        title = '任务完成';
        content = '您发布的任务"' + taskTitle + '"已完成，请及时验收';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '验收任务';
        break;
      case 'verified':
        title = '任务验收通过';
        content = '您完成的任务"' + taskTitle + '"已通过验收，奖励已发放';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '查看详情';
        break;
      case 'rejected':
        title = '任务验收未通过';
        content = '您完成的任务"' + taskTitle + '"验收未通过，请查看反馈并重新完成';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '查看反馈';
        break;
      case 'timeout':
        title = '任务超时提醒';
        content = '您发布的任务"' + taskTitle + '"即将超时，请及时处理';
        actionUrl = '/pages/task/TaskDetail/TaskDetail?id=' + taskId;
        actionText = '处理任务';
        break;
      case 'evaluation_reward': // 新增：评价奖励通知
        const rating = extraData?.rating || 0;
        const points = extraData?.points || 0;
        const evaluationContent = extraData?.evaluationContent || '';
        
        title = '获得评价奖励';
        content = `恭喜！您完成的任务"${taskTitle}"获得了${rating}星评价，获得${points}积分奖励！`;
        
        if (evaluationContent) {
          content += `\n评价内容：${evaluationContent}`;
        }
        
        actionUrl = '/pages/user/user-pointsdetails/user-pointsdetails';
        actionText = '查看积分';
        break;
      default:
        return { success: false, message: '未知的任务状态' };
    }

    // 调用创建通知云函数
    const result = await uniCloud.callFunction({
      name: 'createNotification',
      data: {
        userId: userId,
        type: 'task_status',
        category: 'task',
        title: title,
        content: content,
        relatedId: taskId,
        relatedType: 'task',
        priority: status === 'timeout' ? 'high' : 'normal',
        actionUrl: actionUrl,
        actionText: actionText
      }
    });

    console.log('【createTaskNotification】通知创建结果：', result);

    return result.result || result;

  } catch (e) {
    console.error('【createTaskNotification】异常:', e);
    return { success: false, message: '任务通知创建失败：' + e.message };
  }
};
